package kyssion.leetcode.num101_150;

public class code125_验证回文字符串 {
    public static void main(String[] args) {
        System.out.println(new code125_验证回文字符串().isPalindrome(
                "0P"
        ));
    }

    public boolean isPalindrome(String s) {
        if (s == null || s.equals("")) {
            return true;
        }
        char[] chars = s.toCharArray();
        int start = 0;
        int end = chars.length - 1;
        for (int a = 0; a < chars.length; a++) {
            if (chars[a] >= 'A' && chars[a] <= 'Z') {
                chars[a] = (char) (chars[a] - 'A' + 'a');
            } else if (chars[a] >= 'a' && chars[a] <= 'z' || chars[a] >= '0' && chars[a] <= '9') {
                continue;
            } else {
                chars[a] = ' ';
            }
        }
        while (start < end) {
            if (isChar(chars[start]) && isChar(chars[end])) {
                if (chars[start] != chars[end]) {
                    return false;
                } else {
                    start++;
                    end--;
                }
                continue;
            }
            if (!isChar(chars[start])) {
                start++;
            }

            if (!isChar(chars[end])) {
                end--;
            }
        }
        return true;
    }

    public boolean isChar(char c) {
        return c >= 'a' && c <= 'z' || c >= '0' && c <= '9';
    }
}
